package com.atguigui.leetcode1;

import java.util.Arrays;

/**
 * 6918.包含三个字符串的最短字符串
 * Project: leetcode
 * Package: com.atguigui.leetcode1
 * Version: 1.0
 * <p>
 * Created by WJX on 2023/7/31 10:00
 */
public class P6918ShortestStringThatContainsThreeStrings {


    class Solution {
        public String minimumString(String a, String b, String c) {
            String s1 = join(join(a, b), c);
            String s2 = join(join(a, c), b);
            String s3 = join(join(b, c), a);
            String s4 = join(join(b, a), c);
            String s5 = join(join(c, a), b);
            String s6 = join(join(c, b), a);

            String res = s1;
            for (String str : Arrays.asList(s1, s2, s3, s4, s5, s6)) {
                if (str.length() < res.length()) {
                    res = str;
                } else if (str.length() == res.length() && str.compareTo(res) < 0) {
                    res = str;
                }
            }

            return res;
        }

        private String join(String a, String b) {
            if (a.contains(b)) return a;

            int w = Math.min(a.length(), b.length());
            for (; w > 0; w--) {
                if (a.endsWith(b.substring(0, w))) {
                    break;
                }
            }
            return a + b.substring(w);
        }
    }
}
